<?php

/**
 * Created by PhpStorm.
 * User: muhe0
 * Date: 2017/4/17
 * Time: 3:10
 */

namespace LDL\Twspider;

use LDL\Twspider\C\Taskrun;
use Lin\Nsg\Event\WorkEvent;
use Symfony\Component\EventDispatcher\GenericEvent;
use VDB\Spider\Event\SpiderEvents;
use VDB\Spider\Exception\QueueException;


/*autoload or spl_auto can use in thread
/*
 * function __autoload($clzname){
 *         echo $clzname."\n";
 * exit ;
 * }
 */
//set_include_path("/home/yejianfeng/handcode/"); //这里�?要将路径放入include 
//spl_autoload("http"); //寻找/home/yejianfeng/handcode/http.php
class Task extends Taskrun
{
    /**
     * @var Twspider
     */
    //private $spider;
    //private  $uri ;
    function __construct()
    {
        //   $this->worker
        //     $this->uri= $uri;

    }

    public function synctest()
    {
        // $this->synchronized(function () {
        $lock = $this->worker->getLock();
        //    $lock->lock();
        while (
        $da = $this->worker->spider->getQueueManager()->next()) {
            $se = $this->worker->spider->getQueueManager()->count();
            //、   echo \Thread::getCurrentThreadId()." -{$da}-"."{$se}- is size\n";

            $this->logger->info(\Thread::getCurrentThreadId() . " -{$da}-" . "{$se}- is size");
        }

        echo "\n" . count($da) . " is data \n";
        //   $lock->unlock();
        // });
    }


    //do job
    protected function sleepd()
    {
        $sp = $this->worker->spider;

        if ($this->waitplus) {
            $lock = $this->worker->getLock();
           // $lock->lock();
            $this->waitsec += 3;
            if ($this->waitsec > $this->maxwaitsec) {
                $this->waitsec = $this->maxwaitsec;
            }

           // $lock->unlock();
            $this->logger->debug("pause machen " . $this->waitsec . " sekunde");
            sleep($sp->started ? $this->waitsec : 1);
        }
        return true;
    }

    protected $waitsec = 3;
    protected $maxwaitsec = 60;
    protected $waitplus = true;

    private function initwait()
    {
        $this->waitsec = 3;
    }

    public function run()
    {
        parent::run();
        //$a->sort();

        $lock = $this->worker->getLock();

        //       $this->synctest();
        $log = $this->logger;
        $queue = $this->worker->spider->getQueueManager();
        $dorun = true;
        while ($dorun) {
            if ($this->worker->spider->getDownloader()->isDownLoadLimitExceeded()) {
                $log->warn('download is over limit ,pls wait ');
                $this->sleepd();

                continue;
            }
            $lock->lock();
            $currentUri = $queue->next();
            $lock->unlock();

            if (!$currentUri) {
                //var_dump($this->worker->spider->getDispatcher()->getListeners());
                $log->debug("uri sets is empty or not correct,pls wait");
                $rev = $this->sleepd();
                if ($rev == false) {
                    $log->debug("task exit,Threadid:" . $this->worker->getThreadId());
                    break;
                }
                continue;
            } else {
                $this->initwait();
            }
            /* var_dump($this->worker->spider->getDispatcher()->getListeners(SpiderEvents::SPIDER_CRAWL_PRE_REQUEST));
             var_dump($this->worker->spider->getDispatcher()->getListeners(SpiderEvents::SPIDER_CRAWL_POST_REQUEST));*/
            $log->debug("process url :" . $currentUri);
            //debug($this->worker->spider->getDownloader()) ;
            /*try{
                $lock->lock();*/
            if (!$resource = $this->worker->spider->getDownloader()->download($currentUri)) {
                $log->warn("$currentUri request is fail ");
                continue;
            }
            /*}finally{
                $lock->unlock();
            }*/

            //    echo $this->worker->getCurrentThreadId()." is crawid tthreaadid \n ";
            $this->dis(SpiderEvents::SPIDER_CRAWL_RESOURCE_PERSISTED, new WorkEvent($this->worker, array('uri' => $currentUri, 'resource' => $resource)));
            //下面的 逻辑 应该 丢给 业务程序 (DiscovererSet have filter ,)
            // Once the document is enqueued, apply the discoverers to look for more links to follow
            $discoveredUris = $this->worker->spider->getDiscovererSet()->discover($resource, $this->worker);
            $this->processUris($discoveredUris, $queue);
        }
    }

    protected function processUris(&$discoveredUris, &$queue)
    {
        foreach ($discoveredUris as $uri) {
            try {
                $this->logger->debug($uri);
                //$queue->addUri($uri);
            } catch (QueueException $e) {
                $this->logger->warn($e->getMessage());
                break;
            }
        }
    }

}
